package org.scau.studentevaluation.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.scau.studentevaluation.client.ActClient;
import org.scau.studentevaluation.client.AttendedStudentListClient;
import org.scau.studentevaluation.client.OtherActClient;
import org.scau.studentevaluation.client.StudentClient;
import org.scau.studentevaluation.config.global.log.WebLog;
import org.scau.studentevaluation.entity.ExamQuery;
import org.scau.studentevaluation.entity.ExamVO;
import org.scau.studentevaluation.entity.dos.UserDO;
import org.scau.studentevaluation.service.IExamServicePart2;
import org.scau.studentevaluation.service.ImportMapperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author: x1aolone
 * @Date: 2020/8/25 11:59
 */
@Slf4j
@RestController
@Api("Exam控制器")
@WebLog("Exam控制器")
public class ExamPart2Controller {


    @Autowired
    private IExamServicePart2 examService;

    @GetMapping("/examNum")
    @ApiOperation("统计examNum")
    public int getExamNum(String college){
        return examService.examedNum(college);
    }

    /**
     * Excel表格导出接口
     * http://localhost:port/ExcelDownload
     * @param response response对象
     * @throws IOException 抛IO异常
     */
    @GetMapping("/ExcelDownload")
    @ApiOperation("下载excel表格")
    public void excelDownload(String college, HttpServletResponse response) throws IOException {

        HSSFWorkbook workbook = examService.getWorkbook(college);

        //准备将Excel的输出流通过response输出到页面下载
        //八进制输出流
        response.setContentType("application/octet-stream");

        //这后面可以设置导出Excel的名称，此例中名为student.xls
        response.setHeader("Content-disposition", "attachment;filename=student.xls");

        //刷新缓冲
        response.flushBuffer();
        //workbook将Excel写入到response的输出流中，供页面下载
        workbook.write(response.getOutputStream());
    }


    @GetMapping("/listExamByOtherActId/{otherActId}")
    @ApiOperation("通过otherActId获取exam列表")
    public List<ExamVO> listExamByOtherActId(Long otherActId) {
        return examService.listExamByOtherActId(otherActId);
    }

    @GetMapping("/listExamByActId/{actId}")
    @ApiOperation("通过ActId获取exam列表")
    public List<ExamVO> listExamByActId(Long actId) {
        return examService.listExamByActId(actId);
    }

    @GetMapping("/listActId")
    @ApiOperation("通过学院、规则年份、是否通过获取actId列表")
    public List<Long> listActId(ExamQuery query) {
        return examService.listActId(query);
    }

}
